home *** CD-ROM | disk | FTP | other *** search
- /***** BEGIN LICENSE BLOCK *****
- - Version: MPL 1.1/GPL 2.0/LGPL 2.1
- -
- - The contents of this file are subject to the Mozilla Public License Version
- - 1.1 (the "License"); you may not use this file except in compliance with
- - the License. You may obtain a copy of the License at
- - http://www.mozilla.org/MPL/
- -
- - Software distributed under the License is distributed on an "AS IS" basis,
- - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- - for the specific language governing rights and limitations under the
- - License.
- -
- - The Original Code is "FlashGot".
- -
- - The Initial Developer of the Original Code is Giorgio Maone.
- - Portions created by the Initial Developer are Copyright (C) 2004
- - the Initial Developer. All Rights Reserved.
- -
- - Contributor(s): Giorgio Maone <g.maone @ informaction.com>
- -
- - Alternatively, the contents of this file may be used under the terms of
- - either the GNU General Public License Version 2 or later (the "GPL"), or
- - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
- - in which case the provisions of the GPL or the LGPL are applicable instead
- - of those above. If you wish to allow use of your version of this file only
- - under the terms of either the GPL or the LGPL, and not to allow others to
- - use your version of this file under the terms of the MPL, indicate your
- - decision by deleting the provisions above and replace them with the notice
- - and other provisions required by the LGPL or the GPL. If you do not delete
- - the provisions above, a recipient may use your version of this file under
- - the terms of any one of the MPL, the GPL or the LGPL.
- -
- - ***** END LICENSE BLOCK *****/
-
-
-
- function FlashGotGalleryBuilder() {
- this.expressions={};
- }
-
- FlashGotGalleryBuilder.INTERVAL_RX=/\[\s*(\d+)\s*-\s*(\d+)\s*(;{0,1}\s*\d*)\s*\]/;
- FlashGotGalleryBuilder.INTERVAL_AZ_RX=/\[\s*([a-z]{1})\s*-\s*([a-z]{1})\s*(;{0,1}\s*\d*)\s*\]/i;
- FlashGotGalleryBuilder.EXPR_RX=/\[\s*([\w]+)\s*\((.*?)\)\s*\]/i;
-
- FlashGotGalleryBuilder.prototype = {
- log: function(msg) {
- window.dump("<FLASHGOT_GB>\n"+msg+"\n</FLASHGOT_GB>\n");
- }
- ,
- onload: function() {
- try {
- var data=window.arguments[0];
- this.previewTextBox.value=data.previewURL;
- this.contentTextBox.value=data.contentURL;
- this.referrerTextBox.value=data.referrerURL;
- this.originalWindow=data.originalWindow;
- this.tmpDir=data.tmpDir;
- this.filePath=null;
- this.expressions=data.srcDocument._flashgotGB_expressions;
- if(!this.expressions) this.expressions={};
- this.normalizeURL(this.previewTextBox);
- this.normalizeURL(this.contentTextBox);
- this.validateURLs();
- this.dialog.centerWindowOnScreen();
- } catch(e) {
- this.dialog.cancelDialog();
- }
- }
- ,
- get dialog() {
- return document.documentElement;
- }
- ,
- get previewBase() {
- return this.trim(this.previewTextBox.value);
- }
- , get contentBase() {
- return this.trim(this.contentTextBox.value);
- }
- , get referrer() {
- return this.trim(this.referrerTextBox.value);
- }
- ,
- get previewTextBox() {
- return document.getElementById("flashgotGB-preview-text");
- }
- ,
- get contentTextBox() {
- return document.getElementById("flashgotGB-content-text");
- }
- ,
- get referrerTextBox() {
- return document.getElementById("flashgotGB-referrer-text");
- }
- ,
- get urlsListBox() {
- return document.getElementById("flashgotGB-urls-list");
- }
- ,
- get urlsPreviewDoc() {
- return document.getElementById("flashgotGB-urls-preview").contentWindow.document;
- }
- ,
- get exprListBox() {
- return document.getElementById("flashgotGB-expr-list");
- }
- ,
- get exprTextBox() {
- return document.getElementById("flashgotGB-expr-text");
- }
- ,
- trim: function(s) {
- return s.replace(/^\s+/g,"").replace(/\s+$/g,"");
- }
- ,
- checkIntervals: function(url) {
- return url.search(FlashGotGalleryBuilder.INTERVAL_RX)>-1
- || url.search(FlashGotGalleryBuilder.INTERVAL_AZ_RX)>-1
- ;
- }
- ,
- normalizeURL: function(textBox) {
- var url=textBox.value;
- var hasIntervals=this.checkIntervals(url);
- if(!hasIntervals) {
- url=url.replace(/(\{|\(|<)/g,"[").replace(/(\}|\)|>)/g,"]");
- textBox.value=this.checkIntervals(url)
- ?url
- :textBox.value.replace(/(\d+)/g,"[$1-$1;1]");
- }
- }
- ,
- validateURLs: function() {
- var htmlBuilder=new FlashGotGalleryHTML(this);
-
- var oldListBox=this.urlsListBox;
- var container=oldListBox.parentNode;
-
- var listBox=document.createElement("listbox");
- var listCols=document.getElementsByTagName("listcols")[0];
- if(listCols) {
- oldListBox.removeChild(listCols);
- listBox.appendChild(listCols);
- }
- var attrs=oldListBox.attributes;
- for(var j=attrs.length; j-->0;) {
- var attr=attrs[j];
- listBox.setAttribute(attr.nodeName,attr.nodeValue);
- }
-
- container.removeChild(oldListBox);
- container.appendChild(listBox);
-
- htmlBuilder.buildDOM(this.urlsPreviewDoc);
-
- var valid = htmlBuilder.valid;
-
- this.dialog.getButton("accept").setAttribute("disabled",!valid);
-
- if(valid) {
- function createCell(label) {
- var cell=document.createElement('listcell');
- cell.setAttribute("crop","start");
- cell.setAttribute("flex","1");
- cell.setAttribute('label', label?label:"???");
- return cell;
- }
-
- for(var html; html=htmlBuilder.nextFragment();) {
- var item=document.createElement("listitem");
- item.value=html;
- item.appendChild(createCell(htmlBuilder.currentPreviewURL));
- item.appendChild(createCell(htmlBuilder.currentContentURL));
- listBox.appendChild(item);
- }
- }
-
- var exprListBox=this.exprListBox;
- var selectedExpr=exprListBox.selectedItem?exprListBox.selectedItem.label:null;
- while(exprListBox.getRowCount()>0) exprListBox.removeItemAt(0);
- var exprNames=htmlBuilder.exprNames;
- var selectedItem=null;
- for(var j=0, len=exprNames.length; j<len; j++) {
- var item=exprListBox.appendItem(exprNames[j]);
- if(selectedItem==null || item.label==selectedExpr) selectedItem=item;
- }
- if(selectedItem) {
- exprListBox.selectItem(selectedItem);
- }
- this.exprSelected();
-
-
- this.openSelectedURL();
- }
- ,
- openSelectedURL: function() {
- var item=this.urlsListBox.selectedItem;
- this.urlsPreviewDoc.getElementById(FlashGotGalleryHTML.prototype.galleryId).innerHTML =
- item?item.value:"";
- }
- ,
- synchronizePreview: function() {
- this.synchronizeIntervals(this.contentTextBox,this.previewTextBox);
- }
- ,
- synchronizeContent: function() {
- this.synchronizeIntervals(this.previewTextBox,this.contentTextBox);
- }
- ,
- synchronizeIntervals: function(srcBox,dstBox) {
- var isrc=new FlashGotGalleryIterator(this.trim(srcBox.value));
- var idst=new FlashGotGalleryIterator(this.trim(dstBox.value));
- var src=""
- var dst=""
- while(isrc && idst && isrc.valid && idst.valid) {
- dst=dst.concat(
- idst.base.substring(0,idst.match.index)
- ).concat(
- isrc.match[0]
- );
- isrc=isrc.delegate;
- idst=idst.delegate;
- }
- if(idst) dst=dst.concat(idst.base);
- dstBox.value=dst;
- this.validateURLs();
- }
- ,
- build: function() {
- var htmlBuilder=new FlashGotGalleryHTML(this);
-
- const cc=Components.classes;
- const ci=Components.interfaces;
-
- const galFile=cc["@mozilla.org/file/local;1"].createInstance(ci.nsILocalFile);
- galFile.initWithPath(this.tmpDir.path);
- galFile.append("flashgotGB.html");
- galFile.createUnique(0,-1);
-
- this.filePath=galFile.path;
-
- const os=cc["@mozilla.org/network/file-output-stream;1"].createInstance(
- ci.nsIFileOutputStream);
-
- try {
- os.init(galFile,0x02,-1,0);
-
- var html=htmlBuilder.header;
- os.write(html,html.length);
-
- while(html=htmlBuilder.nextFragment() ) {
- os.write(html,html.length);
- if(!htmlBuilder.valid) break;
- }
-
- html=htmlBuilder.footer;
- os.write(html,html.length);
-
- } finally {
- os.close();
- }
- const ios=cc['@mozilla.org/network/io-service;1'].getService(ci.nsIIOService);
-
- var w=this.originalWindow;
- var url=ios.newFileURI(galFile).spec;
- if(w.closed) {
- w=window.open(url,"_blank");
- } else {
- var browser=w.getBrowser();
- browser.selectedTab=browser.addTab(url, null);
- }
- }
- ,
- expr2Func: function(expr) {
- return new Function(expr);
- }
- ,
- func2Expr: function(func) {
- return func.toString().replace(/^\s*function .*\{\n/,"\n").replace(/\n\}\s*$/,"").replace(/\n\s{3}/g,"\n").replace(/^\n/,"");
- }
- ,
- tabSelected: function(ev) {
- switch(ev.target.selectedItem.id) {
- case "flashgotGB-url-tab":
- this.exprChanged();
- break;
- case "flashgotGB-expr-tab":
- default:
- this.validateURLs();
- this.exprTextBox.focus();
- }
- }
- ,
- exprSelected: function() {
- var exprTextBox=this.exprTextBox;
- var exprDes=document.getElementById("flashgotGB-expr-des");
- var rxFxName=/\bfunction \w+\(/;
- var errorTextBox=document.getElementById("flashgotGB-expr-error-text");
- errorTextBox.value="";
- var selectedItem=this.exprListBox.selectedItem;
- if(!selectedItem) {
- exprTextBox.value="";
- exprDes.value=exprDes.value.replace(rxFxName,"function fx(");
- exprTextBox.setAttribute("disabled",true);
- } else {
- exprTextBox.removeAttribute("disabled");
-
- var exprName=[selectedItem.label];
- var expr=this.expressions[exprName];
- exprDes.value=exprDes.value.replace(rxFxName,"function "+exprName+"(");
- exprTextBox.value="return \"\";";
-
- switch(typeof(expr)) {
- case "function":
- exprTextBox.value=this.func2Expr(expr);
- exprTextBox.style.color="black";
- errorTextBox.value=expr.lastError
- ?expr.lastError.name+"\n"+expr.lastError.message
- :"";
- break;
- case "string":
- exprTextBox.value=expr;
- default:
- exprTextBox.style.color="red";
- }
- }
- }
- ,
- exprChanged: function() {
- var selectedItem=this.exprListBox.selectedItem;
- if(!selectedItem) {
- this.exprTextBox.setAttribute("disabled",true);
- } else {
- this.exprTextBox.removeAttribute("disabled");
- var expr=this.exprTextBox.value;
- try {
- expr=this.expr2Func(expr);
- } catch(e) {
- this.exprTextBox.focus();
- document.getElementById("flashgotGB-expr-error-text").value=e.message;
- }
- this.expressions[selectedItem.label]=expr;
- this.validateURLs();
- }
- }
- }
-
-
- function FlashGotGalleryHTML(builder) {
- this.builder=builder;
- this.previews=new FlashGotGalleryIterator(builder.previewBase);
- this.contents=new FlashGotGalleryIterator(builder.contentBase);
- var exprNames=[];
- for(var base=builder.previewBase.concat(builder.contentBase), match=null;
- match=base.match(FlashGotGalleryBuilder.EXPR_RX);
- base=base.substring(match.index+match[0].length)
- ) {
- var name=match[1];
- for(var j=exprNames.length; j-->0 && exprNames[j]!=name;);
- if(j<0) exprNames[exprNames.length]=name;
- }
- this.exprNames=exprNames.sort();
- this.index=0;
- }
-
-
-
- FlashGotGalleryHTML.prototype = {
-
- galleryId: "flashgotGB-gallery"
- ,
- get headElementSource() {
- function jsEscape(s) {
- return (s==null
- ?"null"
- :'"'+s.replace(/([\\"\n\t])/g,"\\$1")+'"'
- );
- }
-
- function jsvar(n,s) {
- return 'document.'+n+'='
- + jsEscape(s) +';';
- }
-
- function serializeExpressions(exprNames,expressions) {
- var code="document._flashgotGB_expressions={\n";
- var props=new Array();
- for(var j=0, len=exprNames.length; j<len; j++) {
- var n=exprNames[j];
- var v=expressions[n];
- if(v) {
- props[props.length]=n + ": "
- + (typeof(v)=="function"?v.toString():jsEscape(v));
- }
- }
- return code.concat(props.join("\n,\n")).concat("\n};");
- }
-
- return '<head><title>'+this.builder.referrer+" - "
- +this.builder.dialog.getAttribute('title')+'</title>\n'
- +'<script type="text/javascript">'
- + jsvar('_flashgotGB_referrer',this.builder.referrer)
- + jsvar('_flashgotGB_previews',this.previews.base)
- + jsvar('_flashgotGB_contents',this.contents.base)
- + serializeExpressions(this.exprNames,this.builder.expressions)
- +'</script>\n'
- +'<style type="text/css">\n'
- +'body,div { font-family: verdana,arial,hevetica,sans-serif; '
- +'font-size: 10px; color: black; background: white }\n}'
- +'a { color: blue; text-decoration: underline; }\n'
- +'</style></head>'
- ;
- }
- ,
- get header() {
- return '<html>'
- +this.headElementSource
- +'<body>\n'
- +'<div id="'+this.galleryId+'">\n'
- ;
- }
- ,
- get footer() {
- return "\n</div></body></html>";
- }
- ,
- evalExpressions: function(iterator) {
- var url=iterator.nextURL();
- if( ! ( url && this.exprNames.length) ) return url;
- var base=url;
- var evalURL="";
- var obj = { index: this.index, baseURL: base };
- for(var match=null;
- match=base.match(FlashGotGalleryBuilder.EXPR_RX);
- base=base.substring(match.index+match[0].length)
- ) {
- var name=match[1];
- var expr=this.builder.expressions[name];
- var subst=match[0];
- if(typeof(expr)=="function") {
- try {
- obj.expr=expr;
- var res=eval("obj.expr("+match[2]+")");
- if(res!=null && typeof(res)!="undefined") {
- subst=res;
- }
- } catch(e) {
- expr.lastError=e;
- }
- }
- evalURL+=base.substring(0,match.index).concat(subst);
- }
- evalURL+=base;
- return evalURL;
- }
- ,
- nextFragment: function() {
-
- var p = this.currentPreviewURL =
- this.evalExpressions(this.previews);
- var c = this.currentContentURL =
- this.evalExpressions(this.contents);
- if(
- (! (p || c) )
- || (p==null && !this.contents.valid)
- || (c==null && !this.previews.valid)
- ) {
- return null;
- }
-
-
- var html=p?'<img src="'+p+'" alt="'+(c?c:"???")+'" />':c+'<br />\n';
- if(c) html='<a href="'+c+'">'+html+'</a>\n';
- return html;
- }
- ,
- get valid() {
- return this.previews.valid || this.contents.valid;
- }
- ,
- reset: function() {
- this.previews.reset();
- this.contents.reset();
- this.index=0;
- }
- ,
- buildDOM: function(doc) {
- if(!doc.getElementById(this.galleryId)) {
- doc.documentElement.innerHTML=this.headElementSource;
- doc.documentElement.appendChild(doc.createElement("body")
- ).appendChild(doc.createElement("div")).id=this.galleryId;
- }
- }
-
- }
-
-
- function FlashGotGalleryIterator(base) {
- this.base=base;
- var match=FlashGotGalleryBuilder.INTERVAL_RX.exec(this.base);
- if(match) {
- this.isAZ=false;
- this.start=parseInt(match[1],10);
- this.end=parseInt(match[2],10);
- this.padding="";
- for(var j=(this.end>this.start?match[1]:match[2]).length;
- j-->0;
- this.padding=this.padding.concat("0")
- );
- this.valid=true;
- } else {
- match=FlashGotGalleryBuilder.INTERVAL_AZ_RX.exec(this.base);
- if(this.isAZ=match!=null) {
- if(/[a-z]{1}/.test(match[1])) {
- match[2]=match[2].toLowerCase();
- } else {
- match[2]=match[2].toUpperCase();
- }
-
- this.start=match[1].charCodeAt(0);
- this.end=match[2].charCodeAt(0);
-
- this.valid=true;
- } else {
- this.valid=false;
- return;
- }
- }
- this.match=match;
-
- var stepMatch=this.match[3].match(/;\s*(\d+)/);
- this.step =
- (stepMatch?parseInt(stepMatch[1],10):1)
- *
- (this.start<=this.end?1:-1)
- ;
-
- this.cursor=this.start;
-
- this.delegate=new FlashGotGalleryIterator(
- this.match.input.substring(this.match.index+this.match[0].length)
- );
- }
-
- FlashGotGalleryIterator.prototype = {
- reset: function() {
- this.cursor=this.start;
- if(this.delegate) this.delegate.reset();
- }
- ,
- nextURL: function() {
- if(!this.valid) return this.base;
-
- if(this.step==0
- || (this.step>0 && this.cursor>this.end)
- || (this.step<0 && this.cursor<this.end)) {
- return null;
- }
-
- var count;
-
- if(this.isAZ) {
- count=String.fromCharCode(this.cursor);
- } else {
- count=new String(this.cursor);
- if(count.length<this.padding.length) {
- count=this.padding.substring(count.length).concat(count);
- }
- }
-
- var delegatePart=this.delegate.nextURL();
- if(delegatePart==null || !this.delegate.valid) {
- this.cursor+=this.step;
- if(delegatePart==null) {
- this.delegate.reset();
- return this.nextURL();
- }
- }
-
- return this.match.input.substring(0,this.match.index
- ).concat(count
- ).concat(delegatePart);
-
- }
-
-
- }
-
- var flashgotGB=new FlashGotGalleryBuilder();
-
-